//用来代替window.onload的方法
function addLoadEvent(func){
    var oldonload = window.onload;
    if(typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            oldonload();
            func();
        }
    }
}
//页面加载完成运行prepareGallery()函数
addLoadEvent(prepareGallery);

//切换图片
function showPic(whichpic){
    //验证是否有placeholder这个id
    if(!document.getElementById('placeholder')){
        return false;
    }
    var source = whichpic.getAttribute('href')
    var placeholder = document.getElementById('placeholder');
    //验证placeholder是否为img标签
    if(placeholder.nodeName != 'IMG'){
        return false;
    }
    placeholder.setAttribute('src',source)
    //验证是否有description这个id
    if(document.getElementById('description')){
        //如果没有title属性，这text为空
        var text = whichpic.getAttribute('title') ? whichpic.getAttribute('title') : '';
        var description = document.getElementById('description');
        //判断description的第一个子节点是否为文本节点
        if(description.firstChild.nodeType === 3){
            description.firstChild.nodeValue = text
        }
    }
    return false;
    
}
//onclick事件
function prepareGallery(){
    //判断是否有document.getElementById属性
    if(!document.getElementById){
        return false
    }
    //判断是否有document.getElementsByTagName属性
    if(!document.getElementsByTagName){
        return false
    }
    //判断是否有id为imagegallery的元素
    if(!document.getElementById('imagegallery')){
        return false
    }
    var gallery = document.getElementById('imagegallery')
    var links = gallery.getElementsByTagName('a')
    console.log(links)
    for(var i = 0;i < links.length;i++){
        links[i].onclick = function(){
            showPic(this)
            //为了禁止a链接的跳转
            return !showPic(this) ? false : true
        }
    }
}